
//
//  HomeView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/15.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "HomeView.h"
#import "HomeModel.h"
#import "CInputBoxView.h"
#import "CompleteView.h"
#import "LoadingView.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import "HomeTableTopCell.h"
#import "BelowHomeTableCell.h"
#import "HomeCell.h"
#import "HomeCenterCell.h"
#import "CModelAction.h"
#import "DataModel.h"
#import <AFNetworking/AFNetworking.h>
#import <AVFoundation/AVFoundation.h>
#import "CompleteTypeView.h"
@interface HomeView()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BMKLocationService* _locService;
    BMKGeoCodeSearch *_geocodesearch;
    BOOL isGeoSearch;
    double longitude;
    double latitude;
    BOOL isInfo;


}
//@property(nonatomic) BOOL isCompletceClick;
@property (nonatomic, strong)AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) AFNetworkReachabilityManager *manager;

@property (nonatomic,strong)UIView *noDataView;
@end
@implementation HomeView
- (void)initView{
    [super initView];
//    self.isCompletceClick=NO;
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    //编码服务的初始化(就是获取经纬度,或者获取地理位置服务)
    _geocodesearch.delegate = self;//设置代理为self
    
    [self setShowNoDataNotice:@"暂无任务"];
    self.backgroundColor =[UIColor clearColor];
    self.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.showsVerticalScrollIndicator =NO;
    [self registerCell:[HomeTableTopCell class] withModel:[HomeModel class]];
    [self registerCell:[HomeCenterCell class] withModel:[HomeModel class]];
    [self registerCell:[BelowHomeTableCell class] withModel:[HomeModel class]];
    [self setDataWithUrl:@"home_pagedOrdersInProgress" andParam:nil];
    [self setTableActionType:CTableActionType_Both];
    self.manager = [AFNetworkReachabilityManager manager];
    __weak typeof(self) weakSelf = self;
    [self.manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(status==AFNetworkReachabilityStatusNotReachable){
            weakSelf.mainData=[HomeModel arrayOfModelsFromDictionaries:[CClass getCacheObject:NSUD_HomeCache] error:nil];
            [weakSelf reloadData];
        }else
        {
            
        }
    }];
    [self.manager startMonitoring];
    [self loadData];
  
 
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scrollView) {
        self.scrollView(scrollView.contentOffset.y);
        return;
    }
    
}

- (void)reloadData
{
    if (self.getMessageStr) {
        self.getMessageStr(self.extData.D_tipsCnt);
    }
//    self.isCompletceClick=NO;
    DataModel *data =self.extData;
    if (data.D_list.count<=10 && data.D_list.count > 0) {
        [CClass saveCacheObject:data.D_list key:NSUD_HomeCache];
    }
    
    [super reloadData];
}
- (void)setWithType:(NSString *)type
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    
    //默认为当前报修
    
    if ([type isEqualToString:@""]) {
        [self setDataWithUrl:@"home_pagedOrdersInProgress" andParam:nil];

    }else
    {
        
        [dic setObject:type forKey:@"state"];
        if ([type isEqualToString:@""]) {
            //当前任务
            [self setShowNoDataNotice:@"暂无任务"];
            
        }else if ([type isEqualToString:@"01"]) {
            //待接单
            [self setShowNoDataNotice:@"没有待接单的任务"];
            
            
            
        }else if ([type isEqualToString:@"02"]) {
            //待处理
            [self setShowNoDataNotice:@"没有待处理的任务"];
            
        }else if ([type isEqualToString:@"04"]) {
            //处理中
            [self setShowNoDataNotice:@"沒有处理中的任务"];
            
            
            
        }else if ([type isEqualToString:@"06"]) {
            //挂起中
            [self setShowNoDataNotice:@"没有挂起中的任务"];
            
            
        }else if ([type isEqualToString:@"20"]) {
            //待确认
            [self setShowNoDataNotice:@"没有待确认的任务"];
            
            
        }
        
        [self setDataWithUrl:@"home_pagedOrdersByState" andParam:dic];
    }
    
   
    
    [self refresh];
    
}

- (void)initData
{
    [super initData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mainData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HomeModel *order = [self.mainData objectAtIndex:section];
    NSInteger num=[order.H_state integerValue];
    //H_order_type 1 为报修单 2为巡检单
    if ([order.H_order_type isEqualToString:@"2"]) {
        
    if (num==2||num==1) {
        return 3;
  
    }else
    {
        return 2;

    }
    }else
    {
        return 3;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeModel *order = [self.mainData objectAtIndex:indexPath.section];
    NSInteger num=[order.H_state integerValue];
    //H_order_type 1 为报修单 2为巡检单
    if ([order.H_order_type isEqualToString:@"2"]) {
    if (num==2||num==1) {
            
        if (indexPath.row==0) {
                return 65;
            }else if(indexPath.row==1) {
   
                HomeModel *order = [self.mainData objectAtIndex:indexPath.section];
                CGFloat equitmetHeight;
                CGFloat textDescridHeight;
                NSString *equitString;
                NSString *textDescride;
                
                if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])&&(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
                    equitString=@"";
                    equitmetHeight=0;
                }else if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])||(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
                    
                    if(order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""]){
                        equitmetHeight=25;
                        equitString=order.H_machine_type_name;
                    }else if(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""]){
                        equitmetHeight=25;
                        equitString=order.H_machine_name;
                        
                    }
                }else
                {
                    equitString =[NSString stringWithFormat:@"%@%@",order.H_machine_type_name,order.H_machine_name];
                    CGSize equitmetSize  = [equitString sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                    equitmetHeight=10+equitmetSize.height;
                }
                
                
                if ((order.H_textdescribe ==nil||[order.H_textdescribe isEqualToString:@""])) {
                    textDescride=@"";
                    textDescridHeight=0;
                }else
                {
                    textDescride=order.H_textdescribe;
                    
                    CGSize textDescridSize = [textDescride sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                    textDescridHeight=textDescridSize.height+10+10;
                    
                    
                }
                NSString*imgStr=order.H_image;
                NSString *videoStr=order.H_video;
                CGFloat imgHeight;
                CGFloat width =(WindowWidth-95-30)/3;
                if (imgStr ==nil||[imgStr isEqualToString:@""]){
                    if(videoStr.length>0){
                        imgHeight=width+10;
                        
                    }else
                    {
                        imgHeight=0;
                    }
                }else
                {
                    NSArray *array= [imgStr componentsSeparatedByString:@","];
                    
                    if (array.count>2&&videoStr.length>0) {
                        imgHeight=(width+10)*2+10;
                    }else
                    {
                        imgHeight=width+10;
                        
                    }
                }
                CGFloat audioHeight;
                if ((order.H_audio ==nil||[order.H_audio isEqualToString:@""])) {
                    audioHeight=0;
                }else
                {
                    CGFloat videocompressHeight;
                    if ((order.H_audio_translator ==nil||[order.H_audio_translator isEqualToString:@""])) {
                        videocompressHeight=0;
                    }else
                    {
                        NSString *string=[NSString stringWithFormat:@"(%@)",order.H_audio_translator];
                        
                        CGSize  videocompressSize  = [string sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                        videocompressHeight=5+videocompressSize.height;
                    }
                    
                    audioHeight=30+10+videocompressHeight;
                }
                return MAX((equitmetHeight)+(textDescridHeight)+5+imgHeight+(audioHeight), 15);
            }else
            {
                return 35;
                    
            }
        }else
        {
            
            if (indexPath.row==0) {
                return 65;
            }else  {
                
                HomeModel *order = [self.mainData objectAtIndex:indexPath.section];
                CGFloat equitmetHeight;
                CGFloat textDescridHeight;
                NSString *equitString;
                NSString *textDescride;
                
                if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])&&(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
                    equitString=@"";
                    equitmetHeight=0;
                }else if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])||(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
                    
                    if(order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""]){
                        equitmetHeight=25;
                        equitString=order.H_machine_type_name;
                    }else if(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""]){
                        equitmetHeight=25;
                        equitString=order.H_machine_name;
                        
                    }
                }else
                {
                    equitString =[NSString stringWithFormat:@"%@%@",order.H_machine_type_name,order.H_machine_name];
                    CGSize equitmetSize  = [equitString sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                    equitmetHeight=10+equitmetSize.height;
                }
                
                
                if ((order.H_textdescribe ==nil||[order.H_textdescribe isEqualToString:@""])) {
                    textDescride=@"";
                    textDescridHeight=0;
                }else
                {
                    textDescride=order.H_textdescribe;
                    
                    CGSize textDescridSize = [textDescride sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                    textDescridHeight=textDescridSize.height+10+10;
                    
                    
                }
                NSString*imgStr=order.H_image;
                NSString *videoStr=order.H_video;
                CGFloat imgHeight;
                CGFloat width =(WindowWidth-95-30)/3;
                if (imgStr ==nil||[imgStr isEqualToString:@""]){
                    if(videoStr.length>0){
                        imgHeight=width+10;
                        
                    }else
                    {
                        imgHeight=0;
                    }
                }else
                {
                    NSArray *array= [imgStr componentsSeparatedByString:@","];
                    
                    if (array.count>2&&videoStr.length>0) {
                        imgHeight=(width+10)*2+10;
                    }else
                    {
                        imgHeight=width+10;
                        
                    }
                }
                CGFloat audioHeight;
                if ((order.H_audio ==nil||[order.H_audio isEqualToString:@""])) {
                    audioHeight=0;
                }else
                {
                    CGFloat videocompressHeight;
                    if ((order.H_audio_translator ==nil||[order.H_audio_translator isEqualToString:@""])) {
                        videocompressHeight=0;
                    }else
                    {
                        NSString *string=[NSString stringWithFormat:@"(%@)",order.H_audio_translator];
                        
                        CGSize  videocompressSize  = [string sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                        videocompressHeight=5+videocompressSize.height;
                    }
                    
                    audioHeight=30+10+videocompressHeight;
                }
                return MAX((equitmetHeight)+(textDescridHeight)+5+imgHeight+(audioHeight), 15);
            }

        }
    }else{
    
    if (indexPath.row==0) {
   
        return 65;
    }else if(indexPath.row==1) {
        
        HomeModel *order = [self.mainData objectAtIndex:indexPath.section];
        CGFloat equitmetHeight;
        CGFloat textDescridHeight;
        NSString *equitString;
        NSString *textDescride;
        
        if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])&&(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
            equitString=@"";
            equitmetHeight=0;
        }else if ((order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""])||(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""])) {
            
            if(order.H_machine_name ==nil||[order.H_machine_name isEqualToString:@""]){
                equitmetHeight=25;
                equitString=order.H_machine_type_name;
            }else if(order.H_machine_type_name ==nil||[order.H_machine_type_name isEqualToString:@""]){
                equitmetHeight=25;
                equitString=order.H_machine_name;
                
            }
        }else
        {
            equitString =[NSString stringWithFormat:@"%@%@",order.H_machine_type_name,order.H_machine_name];
            CGSize equitmetSize  = [equitString sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
            equitmetHeight=10+equitmetSize.height;
        }
        
        
        if ((order.H_textdescribe ==nil||[order.H_textdescribe isEqualToString:@""])) {
            textDescride=@"";
            textDescridHeight=0;
        }else
        {
            textDescride=order.H_textdescribe;
            
            CGSize textDescridSize = [textDescride sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
            textDescridHeight=textDescridSize.height+10+10;
            
            
        }
        NSString*imgStr=order.H_image;
        NSString *videoStr=order.H_video;
        CGFloat imgHeight;
        CGFloat width =(WindowWidth-95-30)/3;
        if (imgStr ==nil||[imgStr isEqualToString:@""]){
            if(videoStr.length>0){
                imgHeight=width+10;
                
            }else
            {
                imgHeight=0;
            }
        }else
        {
            NSArray *array= [imgStr componentsSeparatedByString:@","];
            
            if (array.count>2&&videoStr.length>0) {
                imgHeight=(width+10)*2+10;
            }else
            {
                imgHeight=width+10;
                
            }
        }
        CGFloat audioHeight;
        if ((order.H_audio ==nil||[order.H_audio isEqualToString:@""])) {
            audioHeight=0;
        }else
        {
            CGFloat videocompressHeight;
            if ((order.H_audio_translator ==nil||[order.H_audio_translator isEqualToString:@""])) {
                videocompressHeight=0;
            }else
            {
                NSString *string=[NSString stringWithFormat:@"(%@)",order.H_audio_translator];
                
                CGSize  videocompressSize  = [string sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-95, 900)];
                videocompressHeight=5+videocompressSize.height;
            }
            
            audioHeight=30+10+videocompressHeight;
        }
        return MAX((equitmetHeight)+(textDescridHeight)+5+imgHeight+(audioHeight), 15);
    }else
    {
        return 35;
        
    }
}



}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor =BackColor;
    UILabel*belowline =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
    [view addSubview:belowline];
    
    belowline.backgroundColor = C(@"E5E5E5");

    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;

    }else
    {
        return 5;

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *classarray = @[[HomeTableTopCell class],[HomeCenterCell class],[BelowHomeTableCell class]];
    Class class = classarray[indexPath.row];
    if (class == nil) {
        class = [HomeCell class];
    }
    NSString *cellID = [NSString stringWithFormat:@"Cell_%@", NSStringFromClass(class)];
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[class alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (self.mainData.count == 0) {
        [cell setModel:nil];
        return cell;
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    [cell setModel:self.mainData[indexPath.section]];
//    if ([class isSubclassOfClass:[HomeCenterCell class]]) {
//        
//        [cell.soundView bk_whenTapped:^{
//            if (self.btnBlock) {
//                self.btnBlock(self,HomeCellType_Sound,nil);
//            }
//            
//            
//            
//        }];
//        
//
//    }
 
    [cell setBtnBlock:^(HomeCell *c, HomeCellType type, NSString *imgNUm) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlayer" object:nil];

        NSIndexPath *path = [tableView indexPathForCell:c];
        HomeModel *data = self.mainData[path.section];
        NSInteger num=[data.H_state integerValue];

        
        if (type==HomeCellType_Relpay) {//回复
            if (self.goToNextDetail) {
                if (self.mainData.count == 0) {
                    self.goToNextDetail(nil);
                    return;
                }
                self.goToNextDetail(data);
            }
        }else if(type==HomeCellType_Add)//补充设备
        {
            if (self.CCellGotoAdd) {
                if (self.mainData.count == 0) {
                    self.CCellGotoAdd(nil);
                    return;
                }
                self.CCellGotoAdd(data);
            }
        }else  if (type==HomeCellType_Img) //图片
        {
            if (self.blowUpImg) {
                if (self.mainData.count == 0) {
                    self.blowUpImg(nil,nil);
                    return;
                }
                self.blowUpImg(data,imgNUm);
            }
            
        }else if (type==HomeCellType_Sound)//声音
        {
            NSString*urlString =[CClass getCacheData:qnDomain];
            for (int i = 0; i < self.mainData.count; i ++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:i];
                HomeCenterCell *cell = [self cellForRowAtIndexPath:path];
                [cell stopPlayer];
            }
            [self.audioPlayer stop];
            self.audioPlayer = nil;
            //1.音频文件的url路径
            NSURL *voiceUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlString,data.H_audio]];
            //远程地址
            NSURL *URL = voiceUrl;
            //默认配置
            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            
            //AFN3.0+基于封住URLSession的句柄
            AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
            
            //请求
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            
            //下载Task操作
            NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                
                //- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径
                
                NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
                NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
                return [NSURL fileURLWithPath:path];
                
            } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
                //设置下载完成操作
                // filePath就是你下载文件的位置，你可以解压，也可以直接拿来使用
                NSData * audioData = [NSData dataWithContentsOfURL:filePath];
                //播放录音
                AVAudioSession *seesion= [AVAudioSession sharedInstance];
                //混音播放
                
                [seesion setCategory:AVAudioSessionCategoryPlayback error:nil];
                [seesion setActive:YES error:nil];
                
                self.audioPlayer=[[AVAudioPlayer alloc]initWithData:audioData error:nil];
                if (self.audioPlayer  == nil)
                {
                    echo(([NSString stringWithFormat:@"ERror creating player: %@", [error description]]));
                }else{
                    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayer:) name:@"stopPlayer"object:nil];
                    [c startAnimate:[NSString stringWithFormat:@"%ld s",(long)self.audioPlayer .duration]];
                    
                    //准备播放
                    [self.audioPlayer  prepareToPlay];
                    //播放
                    [self.audioPlayer  play];
                    //            //开始动画
//                    [cell.soundImg startAnimating];
                    __block  NSInteger duration = (NSInteger)self.audioPlayer.duration;
                    NSTimeInterval period = 1.0; //设置时间间隔
                    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
                    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
                    dispatch_source_set_event_handler(_timer, ^{
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                        });
                        if (duration -- <= 0 ) {
                            dispatch_source_cancel(_timer);
                            dispatch_async(dispatch_get_main_queue(), ^{
                                
                                //关闭动画
                                [c stopPlayer];
                                
                            });
                        }
                    });
                    dispatch_resume(_timer);
                    
                }
            }];
            [downloadTask resume];

        }else if (type==HomeCellType_Detail)//详情
        {
            //H_order_type 1 为报修单 2为巡检单
            if ([data.H_order_type isEqualToString:@"1"]) {
                if (self.goToNextDetail) {
                    if (self.mainData.count == 0) {
                        self.goToNextDetail(nil);
                        return;
                    }
                    self.goToNextDetail(data);
                }

            }else
            {
                if (self.goToNextContextPolling) {
                    if (self.mainData.count == 0) {
                        self.goToNextContextPolling(nil);
                        return;
                    }
                    self.goToNextContextPolling(data);
                }
 
            }

           
        }if (type ==HomeCellType_Progress)//进度 报修单
        {
            //H_order_type 1 为报修单 2为巡检单
            
            if ([data.H_order_type isEqualToString:@"1"]) {
                if(num ==1){
                    //   待接单
                    //    显示接单请求
                    
                    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                    [dic setObject:data.H_uuid forKey:@"orderUuid"];
                    [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                    [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                    if (self.customAccept) {
                        if (self.mainData.count == 0) {
                            self.customAccept(nil);
                            return;
                        }
                        self.customAccept(dic);
                    }
                    

                    
                }else if(num==2){
                    //       待处理
                    //       显示开始 进入下一页
                    if (self.goToNextProgress) {
                        if (self.mainData.count == 0) {
                            self.goToNextProgress(nil);
                            return;
                        }
                        self.goToNextProgress(data);
                    }
                    
                    
                }else  if(num==4||num==5){
                    //处理中
                    //显示标记完成按钮
                    //待挂起
                    //一样---> 显示标记完成按钮
                    NSString *addType;
                    if ((data.H_machine_name ==nil||[data.H_machine_name isEqualToString:@""])||(data.H_machine_type_name ==nil||[data.H_machine_type_name isEqualToString:@""])){
                        isInfo=NO;
                        //没有名字和类型
                        if ((data.H_machine_name ==nil||[data.H_machine_name isEqualToString:@""])&&(data.H_machine_type_name ==nil||[data.H_machine_type_name isEqualToString:@""])) {
                            addType=@"请先补充报修设备！";
                        }else
                        {
                            addType=@"请对报修设备补充设备信息！";
                        }
                    }else
                    {
                        isInfo=YES;
                        
                    }
                    
                    if (isInfo==NO) {
                        [CClass showMessage:addType];
                        return;
                    }
//                    if (self.isCompletceClick==YES) {
//                        
//                    }else
//                    {
//                        self.isCompletceClick=YES;
                        [[LoadingView shareLoading] startAnimating];

//                        [CClass HttpRequestTask:YES withUrl:@"select_showFaultTypeSelect" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
//                            NSMutableArray*typeAr=(NSMutableArray*)model.B_data;
//                            [[LoadingView shareLoading] startAnimating];
                            [CompleteTypeView showTypeAr:nil Action:^(CompleteTypeView *boxView, NSInteger index) {
                                
                                if (index==1) {
                                    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                                    [dic setObject:data.H_uuid forKey:@"orderUuid"];
                                    [dic setObject:boxView.typeData forKey:@"faultTypeUuid"];
                                    [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                                    [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                                    //完成
                                    if (self.gotoComplete) {
                                        if (self.mainData.count == 0) {
                                            self.gotoComplete(nil);
                                            return;
                                        }
                                        self.gotoComplete(dic);
                                    }
//                                    self.isCompletceClick=NO;

                                }
                                [[LoadingView shareLoading] stopAnimating];
                                
                            }];
                    
//                        } onError:^(BaseModel *model) {
//                            
//                        }];
//                        

//                    }
                    

                    
                }else if(num==6){
                    //挂起中
                    //恢复请求
                    
                    [CClass showAlertTitle:nil hidden:NO context:@"确认恢复？" oneBtn:@"确定"twoBtn:@"取消" handler:^{
                        [[LoadingView shareLoading] startAnimating];
                        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                        [dic setObject:data.H_uuid forKey:@"orderUuid"];
                        [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                        [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                        [CClass HttpRequestTask:YES withUrl:@"warning_orderRecoveryByPro" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                            [[LoadingView shareLoading] stopAnimating];
                            [self refresh];
                            [CClass showLittleTimeMessage:@"恢复成功" completion:nil];
                            
                        } onError:^(BaseModel *model) {
                            
                        }];
                        
                    }handler:^{
                        [[LoadingView shareLoading] stopAnimating];
 
                    }];
                    
                }

            }
            
           
            
            else
            {
                
                //巡检单
                if(num ==1){
                    //   待接单
                    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                    [dic setObject:data.H_uuid forKey:@"uuid"];
                    [CClass HttpRequestTask:YES withUrl:@"inspection_accept" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                        [self refresh];
                        [CClass showLittleTimeMessage:@"接单成功" completion:nil];
                  
                    } onError:^(BaseModel *model) {
                        
                    }];
                    
            
                }else if(num==2){
                    // 开始巡检

                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
               [dic setObject:data.H_uuid forKey:@"uuid"];
                    [CClass HttpRequestTask:YES withUrl:@"inspection_start" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                        [self refresh];
                        [CClass showLittleTimeMessage:@"已开始巡检" completion:nil];
                        if (self.mainData.count == 0) {
                            self.goToNextStartContextPolling(nil);
                            return;
                        }
                        self.goToNextStartContextPolling(data);
                    } onError:^(BaseModel *model) {
                        
                    }];

                    
                }

            }
        }

        
            
        
        
     
    }];

    
        
    return cell;
}
- (void)stopPlayer:(NSNotification*)noti
{
    for (int i = 0; i < self.mainData.count; i ++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:i];
        HomeCenterCell *cell = [self cellForRowAtIndexPath:path];
        [cell stopPlayer];
    }
    [self.audioPlayer stop];
    self.audioPlayer = nil;
    
}

#pragma mark 定位
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //关闭地位
    
    
    [self onClickReverseGeocode];
    [_locService stopUserLocationService];
}
-(void)onClickReverseGeocode  //发送反编码请求的.
{
    isGeoSearch = false;
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//初始化
    if (_locService.userLocation.location.coordinate.longitude!= 0
        && _locService.userLocation.location.coordinate.latitude!= 0) {
        //如果还没有给pt赋值,那就将当前的经纬度赋值给pt
        pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude,
            _locService.userLocation.location.coordinate.longitude
        };
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反编码请求
    reverseGeocodeSearchOption.reverseGeoPoint = pt;//设置反编码的店为pt
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//发送反编码请求.并返回是否成功
    if(flag)
    {
        echo(@"反geo检索发送成功");
    }
    else
    {
        echo(@"反geo检索发送失败");
    }
}
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    
    
    for (int i = 0; i < result.poiList.count; i++) {
        
        BMKPoiInfo* poi = [result.poiList objectAtIndex:i];
        if (i==0) {
            latitude= poi.pt.latitude;
            longitude = poi.pt.longitude;
            
        }
    }
}

@end
